//
//  PZRSearchViewController.m
//  Moment
//
//  Created by pang on 15/12/31.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "PZRSearchViewController.h"
#import "PZRSearch.h"
#import "PZRSearchDetailViewController.h"

@interface PZRSearchViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UITableView *mainTableView;
@property (nonatomic,strong) UITextField *searchTextField;
@property (nonatomic,strong) UIButton    *cancelButton;
@property (nonatomic,strong) UIImageView *showImageView;
@property (nonatomic,strong) NSMutableArray *allArray;

@end

@implementation PZRSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.showImageView = [[UIImageView alloc]init];
    self.showImageView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_showImageView];
    
    [self.showImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 60));
        make.top.and.left.mas_equalTo(0);
    }];
    self.searchTextField = [[UITextField alloc]init];
    self.searchTextField.backgroundColor = [UIColor grayColor];
    self.searchTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.searchTextField.delegate = self;
    self.searchTextField.placeholder = @"请输入要搜索的文章或者用户";
    self.searchTextField.textColor = [UIColor redColor];
    [self.view addSubview:_searchTextField];
    [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 60, 60));
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        
    }];
    
    
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.cancelButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    self.cancelButton.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:_cancelButton];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.top.mas_equalTo(0);
        make.left.equalTo(self.searchTextField.mas_right).offset(0);
    }];
    
    [self getData];
    [self createTableView];
    
}
-(void)getData
{
    self.allArray = [NSMutableArray array];
    NSString *para = @"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&version=3.0.6";
    [GetData postUrl:@"http://api2.pianke.me/search/hotlist" body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableArray *bigArr = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in bigArr) {
            PZRSearch *search = [[PZRSearch alloc]init];
            [search setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:search];
        }
        [self.mainTableView reloadData];
        
     
    } failure:^(NSError *error) {
        NSLog(@"失败");
    }];

    
    
}
-(void)createTableView
{
    self.mainTableView = [[UITableView alloc]init];
    //self.mainTableView.backgroundColor = [UIColor redColor];
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"main"];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
    [self.mainTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT -60));
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(60);
    }];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    label.text = @"大家正在搜";
    label.textAlignment = NSTextAlignmentCenter;
    self.mainTableView.tableHeaderView = label;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *chongyong = @"main";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:chongyong];
    PZRSearch *search = [[PZRSearch alloc]init];
    if (self.allArray.count == 0) {
        return nil;
    }
    if (self.allArray.count != 0) {
       search = [self.allArray objectAtIndex:indexPath.row];
       cell.textLabel.text = search.title;
       return cell;

    }
    return nil;
    }
//点击代理方法
-(void)click:(UIButton *)btn
{
    
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    [tempAppDelegate.naVC dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    if ([btn.titleLabel.text isEqualToString:@"确认"]) {
        PZRSearchDetailViewController *detailVC = [[PZRSearchDetailViewController alloc]init];
        [self.navigationController pushViewController:detailVC animated:YES];
    }

    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PZRSearchDetailViewController *detailVC = [[PZRSearchDetailViewController alloc]init];
    PZRSearch *search = [[PZRSearch alloc]init];
    search = [self.allArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if (![self.searchTextField.text isEqualToString:@""]) {
        [self.cancelButton setTitle:@"确认" forState:UIControlStateNormal];
    }
    [self.cancelButton removeFromSuperview];
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.cancelButton setTitle:@"确认" forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
    self.cancelButton.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:_cancelButton];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.top.mas_equalTo(0);
        make.left.equalTo(self.searchTextField.mas_right).offset(0);
    }];

    return YES;
}
-(void)click1:(UIButton *)btn
{
    [self.allArray removeAllObjects];
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&keyword=%@&version=3.0.6", self.searchTextField.text];
    //NSLog(@"!!%@",self.searchTextField.text);
    [GetData postUrl:@"http://api2.pianke.me/search/suggest" body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableArray *bigArr = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in bigArr) {
            PZRSearch *search = [[PZRSearch alloc]init];
            [search setValuesForKeysWithDictionary:dic];
            NSLog(@"%@",search);
            [self.allArray addObject:search];
            NSLog(@"????%@",search.title);
        }
        [self.mainTableView reloadData];
        
        
    } failure:^(NSError *error) {
        NSLog(@"失败");
    }];

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
